﻿/*
 * Copyright 2010 Pieter De Rycke
 *
 * This file is part of Frozen Bubble XNA.
 *
 * Frozen Bubble XNA is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * Frozen Bubble XNA is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Frozen Bubble XNA. If not, see 
 * <http://www.gnu.org/licenses/>.
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * XNA and Windows Phone 7 port:
 *    Pieter De Rycke
 *
 * Websites:
 *    http://www.frozen-bubble.org/
 *    http://pieterderycke.wordpress.com/
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace FrozenBubble
{
    public class LevelSet
    {
        public List<Level> Levels {get; private set;}

        public LevelSet(string name)
        {
            Levels = new List<Level>();

            string data = ReadFile(name);
            string[] lines = data.Split(new char[] { '\n' });
            int rowNumber = 0;
            List<BubbleInformation> levelData = new List<BubbleInformation>();
            foreach (string line in lines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    if (rowNumber > 0)
                    {
                        Levels.Add(new Level(levelData));
                        levelData = new List<BubbleInformation>();
                        rowNumber = 0;
                    }
                }
                else
                {
                    int colNumber = 0;
                    foreach (string item in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (item != "-")
                        {
                            BubbleInformation info;
                            info.cx = colNumber;
                            info.cy = rowNumber;
                            info.imageNumber = Int32.Parse(item);

                            levelData.Add(info);
                        }
                        colNumber++;
                    }
                    rowNumber++;
                }
            }
        }

        private string ReadFile(string path)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            StringBuilder sb = new StringBuilder();
            StreamReader reader = new StreamReader(assembly.GetManifestResourceStream(path));
            string line = reader.ReadLine();
            while (line != null)
            {
                sb.Append(line);
                sb.Append("\n");
                line = reader.ReadLine();
            }
            reader.Close();
            return sb.ToString();
        }
    }

    public class Level
    {
        public List<BubbleInformation> Data {get; private set;}

        public Level(List<BubbleInformation> data)
        {
            Data = data;
        }

        public bool ContainsBubbleNumber(int number)
        {
            foreach (BubbleInformation info in Data)
                if (info.imageNumber == number)
                    return true;
            return false;
        }
    }

    public struct BubbleInformation
    {
        public int cx;
        public int cy;
        public int imageNumber;
    }
}
